package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jayway.jsonpath.internal.function.ParamType;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @Description RoleController
 * @Author apeng
 * @Date 2024-10-24
 */
@RestController
@RequestMapping("role")
@Api(tags = "角色管理")
@Slf4j
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;


    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation("角色分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色dto对象",required = true,dataType = "RoleDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",required = true,example  = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",required = true,example  = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"roleDto.roleName"})   //必传参数
    public ResponseResult<PageResponse<RoleVo>> findRoleVoPage(@RequestBody RoleDto roleDto,
                                                              @PathVariable Integer pageNum,
                                                              @PathVariable Integer pageSize){
        log.info("角色分页，请求参数：{},{},{}",roleDto,pageNum,pageSize);
       return roleService.findRoleVoPage(roleDto,pageNum,pageSize);
    }

    //添加角色
    @ApiOperation("添加角色")
    @ApiImplicitParam(name = "roleDto",
            value = "角色DTO对象",
            required = true,
            dataType = "RoleDto")
    @ApiOperationSupport(includeParameters =
            {"roleDto.roleName","roleDto.dataStatus"})
    @PutMapping
    public ResponseResult createRole(@RequestBody RoleDto roleDto){
        roleService.createRole(roleDto);
        return ResponseResult.success(true);
    }

    //根据角色查询选中的资源数据
    @ApiOperation("根据角色查询选中的资源数据")
    @GetMapping("find-checked-resources/{roleId}")
    public ResponseResult<Set<String>> findCheckedResources(@PathVariable Long roleId){
        Set<String> resources = roleService.findCheckedResources(roleId);
        return ResponseResult.success(resources);
    }

    //角色修改
    @PatchMapping
    @ApiOperation("角色修改")
    @ApiImplicitParam(name = "roleDto",value = "角色修改",required = true,dataType = "RoleDto")
    @ApiOperationSupport(includeParameters = {"roleDto"})
    public ResponseResult<Boolean> updateRole(@RequestBody RoleDto roleDto){
        return roleService.updateRole(roleDto);
    }


    //删除角色
    @DeleteMapping("{roleId}")
    @ApiOperationSupport(includeParameters = {"roleId"})
    @ApiImplicitParam(name = "roleId",value = "删除角色",required = true,dataType = "Long")
    public ResponseResult deleteRole(@PathVariable Long roleId){
        return roleService.deleteRole(roleId);
    }


    //角色下拉框
    @PostMapping("init-roles")
    @ApiOperation("角色下拉框")
    public ResponseResult initRoles(){
        return roleService.initRoles();
    }
}
